#pragma once
#include "framework.h"
#include <time.h>
#include "deflist.h"

using namespace poly;

class Game : public IGame
{
	/*Structures*/
	struct Box
	{
		int player;
		int strength;
		float3 position;
	};
	
public:
	Game( HINSTANCE hInst, HWND hWnd );
	~Game();

	virtual void onCreateGraphics( D3DDevice device );
	virtual void onResetGraphics( D3DDevice device );

	virtual void onFrameMove(float dt);
	virtual void onFrameRender(float dt);

	virtual void onLostGraphics();
	virtual void onDestroyGraphics();

	//Starts the game
	void startGame();
	void startNewRound();

	//Initialize
	void initBall();//Initializes the ball
	void initBox();
	void initPlayers();
	void initPlayer1();
	void initPlayer2();
	void initAI();
	void initInputMouse(); //Call this to initialize mouse
	void initInputMouseStates();//Called from initInputMouse();
	void initText();

	//Create
	void createBox(float3 position);
	void createBox();

	//Remove
	void removeBox();
	void removeBox(std::vector<Box>::iterator& Iter);

	//Update
	void updateBall(float dt);//Determines all ball related operations
	void updateBox();//Determines all box related operations
	void updateAI(); //Determines the movement of AI

	//Hit
	int	 hitBallBox(); //Returns the collision of Ball and Box
	void hitResolveBallBox(Contact contactInfo);

	//Draw
	void drawBall();
	void drawBox();
	void drawText();

	//Input
	void inputCheckMouse(); //Mouse update operations
	void inputMouseToWorld(); //Converts Mouse Device Coordinates to World Coordinates

private:
	D3DDevice	m_d3dDevice;
	HWND m_hWnd; //Handle to window

	SInput		m_input;
	Camera		m_camera;
	D3DVIEWPORT9 dxviewport;

	/*Game Elements============================================/
	/*GAME CONTROL*********************************************/
	//All the variables related to game control
	int gameControl_state; //The state that the game is in

	/*BALL*****************************************************/
	PolygonView			polview_ball;	//PolyVoew
	poly::Polygon		pol_ball;		//Polygon
	float3				pos_ball;		//Position
	float				spd_ball;		//Speed
	float				dir_y_ball;		//Y Direction
	float				dir_x_ball;		//X Direction
	
	/*PLAYERS**************************************************/
	int					box_count_player1;
	int					box_count_player2;
	int					player1_score;
	int					player2_score;

	/*AI*******************************************************/
	int					ai_state;
	bool				ai_canPlaceBox;

	/*BOX****************************************************/
	//A typical 'box' has a strength, initially of maximum 
	//GAME_BOX_MAXSTRENGTH, a position of wherever it is
	//placed and a player that it belongs to.

	PolygonView				polview_box;
	poly::Polygon			pol_box;
	vector<Box>				boxVector;
	bool					add_box;
	bool					remove_box;
	/*======================================================*/

	/*Input Variables=======================================*/
	/*MOUSE**************************************************/
	IInput::MOUSESTATE  inputMouse;
	POINT inputMouseCursor;		 //Mouse Device Coordinates
	float3 inputMouseWorldCoord; //Mouse World Coordinates
	poly::Polygon pol_Mouse_Hit_Area; //Hit Area Polygon
	bool inputMouse1Hold;
	bool inputMouse1Released;
	bool inputMouse2Hold;
	bool inputMouse2Released;
	/*======================================================*/

	//Text output related
	Text textInterface;

	//Temporary variables
};
